package com.forcht.ioc.factory;

import com.forcht.ioc.BeanDefinition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by forcht on 2018/4/17.
 */
public abstract class AbstractBeanFactory implements BeanFactory{

    private Map<String,BeanDefinition> beanDefinitionMap=new ConcurrentHashMap<>();


    @Override
    public Object getBean(String name) {
        return beanDefinitionMap.get(name).getBean();
    }

    @Override
    public void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception{
        Object bean=doCreateBean(beanDefinition);
        beanDefinition.setBean(bean);
        beanDefinitionMap.put(name,beanDefinition);
    }

    /**
     * 创建bean
     * @param beanDefinition
     * @return
     */
    protected  abstract Object doCreateBean(BeanDefinition beanDefinition) throws Exception;
}
